package com.fishery.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.Pond;
import com.fishery.base.entity.vo.TracePondVo;
import com.fishery.base.entity.vo.LeaderPondVo;
import com.fishery.base.entity.vo.PondFarmingVo;
import com.fishery.base.entity.vo.PondVo;
import com.fishery.base.mapper.PondMapper;
import com.fishery.base.service.PondService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@Service
public class PondServiceImpl extends ServiceImpl<PondMapper, Pond> implements PondService {

	@Autowired
	private PondMapper pondMapper;

	@Autowired
	private ExecutorService threadPool;

	@Override
	public Page<PondVo> getAllPondInfoWithPage(String baseId, int size, int current) {
		// 创建一个存放页码信息的对象
		Page<PondVo> page = new Page<>(current, size);
		// 分页查询数据
		Future<List<PondVo>> future = threadPool.submit(() ->
				pondMapper.pageAllPondInfo(baseId, (current - 1) * size, size));
		// 填充总页码数
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("base_id", baseId);
		page.setTotal(pondMapper.selectCount(wrapper));
		// 填充记录
		try {
			page.setRecords(future.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return page;
	}

	@Override
	public LeaderPondVo getPondAmountAndArea(String baseId) {
		LeaderPondVo vo = pondMapper.findPondAmountAndArea(baseId);
		return vo;
	}

	@Override
	public List<PondVo> getAllPondInfo(String baseId) {
		return pondMapper.findAllPondInfo(baseId);
	}

	@Override
	public PondFarmingVo getOnePondInfo(String pondId) {
		return pondMapper.findOnePondInfo(pondId);
	}

	@Override
	public TracePondVo tracePondById(String id) {
		return pondMapper.findPondByIdEvenDelete(id);
	}
}
